package com.cssnj.portal.service.user;

import com.cssnj.portal.domain.common.UserInfo;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

@Service("favoriteService")
public class FavoriteService extends BaseService {

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        switch (jsonRequest.getHandleCode()) {
            case "getFolder":
                return getFolder(jsonRequest);
            case "getGnByFolder":
                return getGnByFolder(jsonRequest);
            case "addGnToFolder":
                return addGnToFolder(jsonRequest);
            case "removeGnFromFolder":
                return removeGnFromFolder(jsonRequest);
            case "removeFolder":
                return removeFolder(jsonRequest);
            case "renameFolder":
                return renameFolder(jsonRequest);
            case "addFolder":
                return addFolder(jsonRequest);
            case "topFolder":
                return topFolder(jsonRequest);

        }
        return null;
    }

    protected JsonResponse getFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));

        String sql = "SELECT SCJ_ID,SCJ_MC FROM GL_GN_SCJ WHERE YH_ID = ? ORDER BY PXXH DESC,LR_SJ DESC ";
        ArrayList<Object> params = new ArrayList<>();
        params.add(usrinfo.getSwrySfDm());
        List<Map<String, Object>> list = this.queryDao.queryForList(sql, params);
        jsonResponse.getRepData().put("scjlist", list);
        return jsonResponse;
    }

    protected JsonResponse getGnByFolder(JsonRequest jsonRequest) throws SQLException, BaseException {
        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");

        ArrayList<Object> params = new ArrayList<>();
        params.add(usrinfo.getSwrySfDm());
        params.add(scj_id);
        List<Map<String, Object>> gnLsit = this.queryDao.queryForList(
                " SELECT B.GN_ID,B.GN_MC, B.HREF,B.OPEN_TYPE, B.JDYS  \n" +
                        "   FROM GL_GN_FAV A, GL_GN B   \n" +
                        "  WHERE A.GN_ID = B.GN_ID      \n" +
                        "    AND B.XY_BJ = '1'          \n" +
                        "    AND YH_ID  =  ?            \n" +
                        "    AND SCJ_ID = ? ", params);

        jsonResponse.getRepData().put("gnLsit", gnLsit);

        return jsonResponse;

    }

    @Transactional
    protected JsonResponse addGnToFolder(JsonRequest jsonRequest) throws SQLException, BaseException {
        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");
        String gn_id = (String) jsonRequest.getReqData().get("gn_id");

        if(scj_id.isEmpty()){
            scj_id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            this.baseDao.getJdbcTemplate().update(" INSERT INTO GL_GN_SCJ (SCJ_MC,YH_ID,LR_SJ,SCJ_ID) VALUES(?,?,SYSDATE,?) ",
                    new Object[]{"我的收藏", usrinfo.getSwrySfDm(), scj_id});
        }

        ArrayList<Object> params = new ArrayList<>();
        params.add(usrinfo.getSwrySfDm());
        params.add(scj_id);
        params.add(gn_id);
        String sql = "select count(1) as gs from GL_GN_FAV a where a.yh_id = ? and a.scj_id = ? and a.gn_id = ?";
        Map map = this.queryDao.queryForMap(sql, params);
        String gs = String.valueOf(map.get("gs"));
        if ("0".equals(gs)) {
            this.baseDao.getJdbcTemplate().update("INSERT INTO GL_GN_FAV (YH_ID,SCJ_ID,GN_ID,LR_SJ) VALUES(?,?,?,SYSDATE) ", params.toArray());
        }
        return jsonResponse;
    }

    protected JsonResponse removeGnFromFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        String gn_id = (String) jsonRequest.getReqData().get("gn_id");
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        this.baseDao.getJdbcTemplate().update("DELETE FROM GL_GN_FAV WHERE GN_ID=? AND SCJ_ID=? AND YH_ID=? ", new Object[]{gn_id, scj_id, usrinfo.getSwrySfDm()});
        return jsonResponse;

    }

    @Transactional
    protected JsonResponse removeFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");
        this.baseDao.getJdbcTemplate().update("DELETE FROM GL_GN_SCJ WHERE SCJ_ID=? AND YH_ID=? ", new Object[]{scj_id, usrinfo.getSwrySfDm()});
        this.baseDao.getJdbcTemplate().update("DELETE FROM GL_GN_FAV WHERE SCJ_ID=? AND YH_ID=? ", new Object[]{scj_id, usrinfo.getSwrySfDm()});

        return jsonResponse;

    }

    protected JsonResponse renameFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");
        String name = (String) jsonRequest.getReqData().get("name");
        this.baseDao.getJdbcTemplate().update(" UPDATE GL_GN_SCJ SET SCJ_MC = ? WHERE YH_ID=? AND SCJ_ID=? ", new Object[]{name, usrinfo.getSwrySfDm(), scj_id});

        return jsonResponse;

    }

    protected JsonResponse addFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String name = (String) jsonRequest.getReqData().get("name");
        String scj_id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        List<Object> params = new ArrayList<>();
        this.baseDao.getJdbcTemplate().update(" INSERT INTO GL_GN_SCJ (SCJ_MC,YH_ID,LR_SJ,SCJ_ID) VALUES(?,?,SYSDATE,?) ",
                new Object[]{name, usrinfo.getSwrySfDm(), scj_id});
        jsonResponse.getRepData().put("scj_id", scj_id);
        return jsonResponse;
    }

    protected JsonResponse topFolder(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String name = (String) jsonRequest.getReqData().get("name");
        String scj_id = (String) jsonRequest.getReqData().get("scj_id");
        String yh_id = usrinfo.getSwrySfDm();
        ArrayList<String> params = new ArrayList<String>();
        params.add(yh_id);
        Map<String, Object> map = this.queryDao.queryForMap(" SELECT NVL(MAX(PXXH),0) PXXH FROM  GL_GN_SCJ WHERE YH_ID=? ", params);
        Integer pxxh = Integer.valueOf(map.get("PXXH").toString());
        this.baseDao.getJdbcTemplate().update(" UPDATE GL_GN_SCJ SET PXXH=? WHERE YH_ID=? AND SCJ_ID=? ", new Object[]{pxxh + 1, yh_id, scj_id});

        return jsonResponse;

    }

}
